package fun.linze.wallpaperwebsite.controller;


import fun.linze.wallpaperwebsite.dao.wallpaper.Tags;
import fun.linze.wallpaperwebsite.dao.wallpaper.WallpaperPutDAO;
import fun.linze.wallpaperwebsite.dao.wallpaper.WallpaperUploadDAO;
import fun.linze.wallpaperwebsite.mapper.TWallpaperMapper;
import fun.linze.wallpaperwebsite.service.TWallpaperService;
import fun.linze.wallpaperwebsite.service.impl.TUserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import fun.linze.wallpaperwebsite.service.impl.TWallpaperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/wallpapers")
public class WallpaperController {

    @Autowired
    private TWallpaperServiceImpl tWallpaperService;
    @Autowired
    private TUserServiceImpl tUserService;

    @Autowired
    private TWallpaperMapper tWallpaperMapper;

//    POST /api/v1/wallpapers
//    功能: 处理上传新壁纸的请求，包括关联初始标签。
    @PostMapping("/upload")
    public  ResponseEntity<?> upload(@RequestBody WallpaperUploadDAO wallpaperUploadDAO,
                                     @RequestHeader("Authorization") String authHeader)  {
        String token = authHeader.replace("Bearer ", "");
        return tWallpaperService.uploadWallpaper(wallpaperUploadDAO,token);
    }
//    GET /api/v1/wallpapers
//    功能: 获取壁纸列表，支持分页、排序和按多个标签过滤。
    @GetMapping
    public ResponseEntity<?> getWallpapers(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "sort", defaultValue = "created_at") String sort,
            @RequestParam(value = "tags", required = false) String[] tags) {
        return tWallpaperService.getWallpapers(page, size, sort, tags);
    }
//    GET /api/v1/wallpapers/{id}
//    功能: 获取单个壁纸的详细信息，并增加其浏览量。
    @GetMapping("/{id}")
    public ResponseEntity<?> getWallpaperById(@PathVariable("id") String id) {

        return tWallpaperService.getWallpaperById(id);
    }
//    PUT /api/v1/wallpapers/{id}
//    功能: 整体更新壁纸的元数据（如标题、描述）。
    @PutMapping("/{id}")
    public ResponseEntity<?> PutWallpaperById(@PathVariable("id") String id,
                                              @RequestBody WallpaperPutDAO wallpaperPutDAO) {

        return tWallpaperService.putWallpaperById(id,wallpaperPutDAO);
    }
//    DELETE /api/v1/wallpapers/{id}
//    功能: 删除指定的壁纸。
    @DeleteMapping("/{id}")
    public ResponseEntity<?> DeleteWallpaperById(@PathVariable("id") String id){
        if(tWallpaperMapper.selectById(id) == null){
            return ResponseEntity.notFound().build();
        }
        tWallpaperMapper.deleteById(id);
        return ResponseEntity.ok().build();
    }
//    POST /api/v1/wallpapers/{id}/like
//    功能: 为指定壁纸点赞。
    @PostMapping("/{id}/like")
    public ResponseEntity<?> LikeWallpaper(@PathVariable("id") String id){
        if(tWallpaperMapper.selectById(id) == null){
            return ResponseEntity.notFound().build();
        }
        tWallpaperService.LikeWallpaper(id);
        return ResponseEntity.ok().build();
    }
//    POST /api/v1/wallpapers/{id}/download
//    功能: 记录对指定壁纸的下载行为。
    @PostMapping("/{id}/download")
    public ResponseEntity<?> DownloadWallpaper(@PathVariable("id") String id){
        if(tWallpaperMapper.selectById(id) == null){
            return ResponseEntity.notFound().build();
        }
        tWallpaperService.DownloadWallpaper(id);
        return ResponseEntity.ok().build();
    }
//    POST /api/v1/wallpapers/{id}/tags
//    功能: 为指定壁纸添加一个或多个新标签。
    @PostMapping("/{id}/tags")
    public ResponseEntity<?> AddWallpaperTags(@PathVariable("id") Long id,
                                              @RequestBody Tags tags) {
        if(tWallpaperMapper.selectById(id) == null){
            return ResponseEntity.notFound().build();
        }
        tWallpaperService.AddWallpaperTags(id,tags);
        return ResponseEntity.ok().body(tags);
    }
//    DELETE /api/v1/wallpapers/{id}/tags
//    功能: 移除指定壁纸与一个或多个标签的关联。
    @DeleteMapping("/{id}/tags")
    public ResponseEntity<?> RemoveWallpaperTags(@PathVariable("id") Long id,
                                                 @RequestBody Tags tags) {
        if(tWallpaperMapper.selectById(id) == null){
            return ResponseEntity.notFound().build();
        }
        tWallpaperService.RemoveWallpaperTags(id,tags);
        return ResponseEntity.ok().body(tags);
    }
}
